#! C:\Perl\bin\perl.exe

# Copyright (c) 2010, College Center for Library Automation

# See the included Copyright.txt file for full license

#
#
#

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use Date::Calc qw(Decode_Month Add_Delta_YM Add_Delta_Days Today);
use Text::CSV;

use avsCommon;



dbsetup();

my ($content, $response, $request, $url);
my $session;

# create user agent, set cookies
my $ua = new LWP::UserAgent(keep_alive=>1);
$ua->cookie_jar( {} );

# access vendor site to get logged in
getLogin();

# capture all the college id's in a hash for use during data extraction
my %college;
do {
	$college {$username} = $collegeid;
} while $sthlogin->fetch;

my ($runyear, $runmonth, $runday) = @begdate;

# run once for each month in optional range. typically beg and end month the same.
while ($runyear < $endyear || $runyear == $endyear && $runmonth <= $endmonth) {
	getStats ();
	($runyear, $runmonth, $runday) = Add_Delta_YM($runyear, $runmonth, $runday, 0, 1);	# advance the month
}

dbshutdown();

exit;

sub getLogin {
	$url = "https://subscriberservices.sams.oup.com/AUTHENTICATE?username=$vendorusername&password=$vendorpassword";
	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET MENU $url : " . $response->status_line;
	};
}

# called once for each period
sub getStats {

	$url = 'https://subscriberservices.sams.oup.com/views/report_single_historical.html';

	my $period = myperiod($runyear, $runmonth);

	$request = HTTP::Request->new(POST => $url);
	$request->content_type('application/x-www-form-urlencoded');
	$request->content("acc_id=10852&start_date_month=$runmonth&start_date_year=$runyear&end_date_month=$runmonth&end_date_year=$runyear&site_id=&statistics_group=0&is_consortial_parent=1&institutions=&format_as=csv&to=onscreen&email=&submit=GENERATE+REPORT");
	
	$response = $ua->request($request);
	if (substr($response->status_line,0,3) eq '500') {
		print "#info: timeout retry\n";
		sleep 5;
		$response = $ua->request($request);
	};
	if (!$response->is_success) {
		die "#error:$0: POST report $url : " . $response->status_line;
	};

	$content = $response->content;
	
	# save contents for possible debugging
	savecontent('Grove', "$runyear-$runmonth", $content);

	my $csv = Text::CSV->new({binary => 1});
	my @contents = split ("\n", $content);

	foreach my $contentline (@contents[1..($#contents-1)]) { # ignore headers and total line

		if ($csv->parse($contentline)) {
			my @columns = $csv->fields();
			# lookup the college id from the grove college number
			if (exists $college{$columns[2]}) {
				my $collegeid  = $college{$columns[2]};
				my $datasource = $columns[0];
				my $session    = $columns[3];
				my $search     = $columns[10];
				print "$vendorid $collegeid $period $datasource $session $search\n";
				$sthstats->execute($vendorid, $collegeid, $datasource, $period, $session, 1)
					or die '#error: sthstats execute 1 failed';
				$sthstats->execute($vendorid, $collegeid, $datasource, $period, $search, 2 )
					or die '#error: sthstats execute 2 failed';
			}

			else {
				print "#error:$0: unknown Grove college number: $columns[1], $columns[2]\n";
			}

		} else {
			my $err = $csv->error_input;
			print "#error:$0: csv parse error date : $err\n";
		};
	}
}